package com.zn
{
    import com.zn.collection.ArrayCollectionDic;
    import com.zn.log.Log;

    import flash.events.NetStatusEvent;
    import flash.net.SharedObject;
    import flash.net.SharedObjectFlushStatus;

    /**
     *用于将数据存储在本地
     * @author zn
     *
     */
    public class LocalSharedObject
    {
        public static var FLUSH_SIZE:uint = 1000;

        private static var _soList:ArrayCollectionDic = new ArrayCollectionDic([ "fileUrl" ]);

        public var fileUrl:String = "";

        public var flushSize:uint = FLUSH_SIZE;

        private var _so:SharedObject;

        public function LocalSharedObject(fileName:String, path:String)
        {
            _so = SharedObject.getLocal(fileName, path);
            fileUrl = path + fileName;

            _soList.addItem(this);
        }

        public static function getLocal(fileName:String, path:String = "/"):LocalSharedObject
        {
            var lso:LocalSharedObject = _soList.getItemByKey("fileUrl", path + fileName) as LocalSharedObject;
            if (!lso)
                lso = new LocalSharedObject(fileName, path);

            return lso;
        }

        public function get data():Object
        {
            return _so.data;
        }

        /**
         *保存数据
         * @param fieldName  属性名
         * @param value
         * @param resultFunction 是否成功的回调方法 方法签名：function(value:Boolean):void
         *
         */
        public function save(fieldName:String, value:Object, resultFunction:Function = null):void
        {
            _so.data[fieldName] = value;

            var flushStatus:String = null;
            try
            {
                flushStatus = _so.flush(flushSize);
            }
            catch (error:Error)
            {
                //保存失败
                if (resultFunction != null)
                    resultFunction(false);

//				trace(LocalSharedObject, "save", "保存失败");
            }

            if (flushStatus != null)
            {
                switch (flushStatus)
                {
                    case SharedObjectFlushStatus.PENDING:
                    {
                        //系统自动弹出是否增加可存储容量对话框
                        var resultFunction:Function;
                        _so.addEventListener(NetStatusEvent.NET_STATUS,
                                             resultFunction = function(event:NetStatusEvent):void
                                             {
                                                 switch (event.info.code)
                                                 {
                                                     case "SharedObject.Flush.Success":
                                                     {
                                                         //保存成功
                                                         if (resultFunction != null)
                                                             resultFunction(true);
//										trace(LocalSharedObject, "save", "保存成功");
                                                         break;
                                                     }
                                                     case "SharedObject.Flush.Failed":
                                                     {
                                                         //保存失败
                                                         if (resultFunction != null)
                                                             resultFunction(false);
//										trace(LocalSharedObject, "save", "保存失败");
                                                         break;
                                                     }
                                                 }

                                                 _so.removeEventListener(NetStatusEvent.NET_STATUS, resultFunction);
                                             });
                        break;
                    }
                    case SharedObjectFlushStatus.FLUSHED:
                    {
                        //保存成功
                        if (resultFunction != null)
                            resultFunction(true);
//						trace(LocalSharedObject,"save","保存成功");
                        break;
                    }
                }
            }
        }

        /**
         *清除全部数据
         *
         */
        public function clear():void
        {
            _so.clear();
        }

        /**
         *删除属性名内容
         * @param fieldName
         *
         */
        public function clearField(fieldName:String):void
        {
            delete data[fieldName];
        }
    }
}
